package edu.hubu.book.dao;

import edu.hubu.book.domain.Book;
import edu.hubu.book.domain.ExtendBook;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author moonlan
 * date 2020/5/3 10:06
 */
@SuppressWarnings(value = "unused")
@Repository
public interface IBookDao {
    /**
     * 根据bid查找图书
     *
     * @param bid 图书的bid
     * @return 返回图书对象
     */
    @Select("select b.*, f.firstClassName, s.secondClassName from book as b inner join firstclass as f on b.sid = f.sid inner join secondclass as s on b.ssid = s.ssid where bid = #{bid};")
    ExtendBook selectOne(@Param("bid") int bid);

    /**
     * 查找所有图书
     *
     * @return 图书的集合
     */
    @Select("select b.*, f.firstClassName, s.secondClassName from book as b inner join firstclass as f on b.sid = f.sid inner join secondclass as s on b.ssid = s.ssid;")
    List<ExtendBook> selectList();

    /**
     * 更改book中的属性
     *
     * @param bid     原来的book的id用于查找该book
     * @param newBook 更改后的book属性
     * @return 受影响的行数
     */
    @Update("update book set bookName = #{newBook.bookName}, imgUrl = #{newBook.imgUrl}, price = #{newBook.price}, discount = #{newBook.discount}, author = #{newBook.author}, publishingHouse = #{newBook.publishingHouse}, sid = #{newBook.sid}, ssid = #{newBook.ssid} where bid = #{bid};")
    int update(@Param("bid") int bid, @Param("newBook") Book newBook);

    /**
     * 添加book
     *
     * @param book 需要添加的book
     * @return 受影响的行数
     */
    @Insert("insert into book(bookName, imgUrl, price, discount, author, publishingHouse, sid, ssid) values(#{book.bookName}, #{book.imgUrl}, #{book.price}, #{book.discount}, #{book.author}, #{book.publishingHouse}, #{book.sid}, #{book.ssid})")
    int addBook(@Param("book") Book book);

    /**
     * 删除bid的图书
     *
     * @param bid 图书的bid
     * @return 受影响的行数
     */
    @Delete("delete from book where bid = #{bid}")
    int delBook(@Param("bid") int bid);
}
